package com.atguigu.gmall.cart.controller;

import com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection;
import com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;


public class test {

    @Test
    public void test() {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "hello", "world", "java", "hao");

        Stream<String> stream = list.stream();
        System.out.println(stream);
    }

    @Test
    public void test1() {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "world");
        map.put(3, "java");

        Set<Integer> keys = map.keySet();
        Stream<Integer> keyStream = keys.stream();
        keyStream.forEach(System.out::println);

        System.out.println("-------------------------");
        Collection<String> values = map.values();
        Stream<String> valueStream = values.stream();
        valueStream.forEach(System.out::println);

        System.out.println("-------------------------");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        Stream<Map.Entry<Integer, String>> entryStream = entries.stream();
        entryStream.forEach(System.out::println);

    }

    @Test
    public void test2() {
        String[] arr = {"hello", "java", "world"};
        Stream<String> stream = Arrays.stream(arr);
        stream.forEach(System.out::println);
    }

    @Test
    public void test3() {
    /*    int[] arr = {1, 2, 3, 4};
        IntStream stream = Arrays.stream(arr);
        stream.forEach(System.out::println);*/



    }


    @Test
    public void test4() {
        Integer[] arr = {1, 2, 3, 4};
        Stream<Integer> stream = Arrays.stream(arr);
        stream.forEach(System.out::println);
    }

    @Test
    public void test5() {
        Stream<String> stream = Stream.of("hello", "java", "world");
        //stream.forEach(System.out::println);
    }

    @Test
    public void test6() {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 1, 2, 3, 4, 5, 6, 9, 11);

        Stream<Integer> stream = list.stream().filter(t -> t % 2 != 0);
        stream.forEach(System.out::println);
    }

    @Test
    public void test7() {
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }

    @Test
    public void test8() {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        list.stream().skip(4).limit(4).forEach(System.out::println);

    }

    @Test
    public void test9() {
        //有一个字符串数组
        String[] arr = {"hello", "Hi", "Abaaab", "Chai", "Jock", "world", "atguigu"};

        //按照字符串大小排序，不区分大小写
        //方案一：Arrays.sort
        //方案二：使用Stream
        Arrays.stream(arr).sorted(String::compareToIgnoreCase).forEach(System.out::println);
    }

    @Test
    public void test10() {
        //随机产生10个[0,100)的整数，然后取出前三名最大的数字
        Stream.generate(() -> (int) (Math.random() * 100)).limit(10).sorted().skip(7).forEach(System.out::println);
    }

    @Test
    public void test08() {
        //随机产生10个[0,100)的整数，然后取出前三名最大的数字
        List<Integer> collect = Stream.generate(() -> (int) (Math.random() * 100))
                .limit(10)
                .peek(System.out::println)
                .sorted()
                .skip(7)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void test09() {
        //随机产生10个[0,100)的整数，然后取出前三名最大的数字
        List<Integer> collect = Stream.generate(() -> (int) (Math.random() * 100))
                .limit(10)
                .peek(System.out::println)
                .sorted((t1, t2) -> t2 - t1)
                .limit(3)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void test11() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        //第一步：创建Stream
        Stream<Integer> stream = list.stream();

        //第三步：终结操作
        boolean result = stream.allMatch(t -> t % 2 == 0);
        System.out.println("result = " + result);

    }

    @Test
    public void test12() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

        //第一步：创建Stream
        Stream<Integer> stream = list.stream();

        //第二步：过滤
        stream = stream.filter(t -> t % 2 == 0);//只留下偶数

        //第三步：终结操作
        boolean result = stream.allMatch(t -> t % 2 == 0);
        System.out.println("result = " + result);
    }

    @Test
    public void test13() {
        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5);

        //第一步：创建Stream
        Stream<Integer> stream = list.stream();

        //第三步：findFirst
        Optional<Integer> first = stream.findFirst(); //Optional是一个容器，它用来包装返回值结果
        System.out.println("first = " + first);
    }

    @Test
    public void test14() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); // 6,7

        //第一步：创建Stream
        Stream<Integer> stream = list.stream();

        //第二步：留下比5大的整数
        stream = stream.filter(t -> t > 5);

        //第三步：findFirst
        Optional<Integer> first = stream.findFirst(); //Optional是一个容器，它用来包装返回值结果
        System.out.println("first = " + first);//Optional.empty 为空
    }

    @Test
    public void test15() {
        //随机产生10个[0,100)之间的整数，打印10个整数，并统计里面的偶数个数
        long count = Stream.generate(() -> (int) (Math.random() * 100))
                .limit(10)
                .peek(System.out::println)
                .filter(t -> t % 2 == 0)
                .count();
        System.out.println("偶数的个数count = " + count);
    }

    @Test
    public void test16() {
        //随机产生10个[0,100)之间的整数，打印10个整数，并统计里面的偶数个数
        ArrayList<Integer> list = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < 10; i++) {
            int num = (int) (Math.random() * 100);
            list.add(num);
            if (num % 2 == 0) {
                count++;
            }
        }
        System.out.println(list);
        System.out.println("偶数的个数count = " + count);
    }

    @Test
    public void test17() {
        //随机产生10个[0,100)之间的整数，打印10个整数，并且返回最大值
        Optional<Integer> max = Stream.generate(() -> (int) (Math.random() * 100))
                .limit(10)
                .peek(System.out::println)
                .max((t1, t2) -> t1 - t2);
        System.out.println("max = " + max);
    }

    @Test
    public void test18() {
        //随机产生10个[0,10)之间的整数，打印10个整数，累加它们的和
        Optional<Integer> list = Stream.generate(() -> (int) (Math.random() * 10))
                .limit(10)
                .peek(System.out::println)
                .reduce((t1, t2) -> t1 + t2);
        /*
        reduce（BinaryOperator b）形参BinaryOperator<T>是函数式接口，抽象方法T apply(T t,  T u)
         */
        System.out.println("list = " + list);
    }

    @Test
    public void test19() {
        //随机产生10个[0,10)之间的整数，打印10个整数，收集其中的偶数
        List<Integer> list = Stream.generate(() -> (int) (Math.random() * 10))
                .limit(10)
                .peek(System.out::println)
                .filter(t->t%2==0)
                .collect(Collectors.toList());
        System.out.println("list = " + list);
    }

    @Test
    public void test21() {
        //随机产生10个[0,10)之间的整数，打印10个整数，收集其中的偶数，不能重复
        List<Integer> list = Stream.generate(() -> (int) (Math.random() * 10))
                .limit(10)
                .peek(System.out::println)
                .filter(t->t%2==0)
                .distinct()
                .collect(Collectors.toList());
        System.out.println("list = " + list);
    }
    @Test
    public void test20() {
        //随机产生10个[0,10)之间的整数，打印10个整数，收集其中的偶数，不能重复
        Set<Integer> set = Stream.generate(() -> (int) (Math.random() * 10))
                .limit(10)
                .peek(System.out::println)
                .filter(t -> t % 2 == 0)
                .collect(Collectors.toSet());
        System.out.println("set = " + set);
    }

    @Test
    public void sws(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入数字{1-10}：");
        String in = input.next();

        switch (in) {
            case "1":
                System.out.println("1");
                break;
            case "2":
                System.out.println("2");
                break;
            case "3":
                System.out.println("3");
                break;
            case "4":
                System.out.println("4");
                break;
            case "5":
                System.out.println("5");
                break;
            case "6":
                System.out.println("6");
                break;
            case "7":
                System.out.println("7");
                break;
            case "8":
                System.out.println("8");
                break;
            case "9":
                System.out.println("9");
                break;
            case "10":
                System.out.println("10");
                break;
            default:
                System.out.println("输入错误");
                break;
        }

    }



    public static void main(String[] args) {


    }



}

